<?php
namespace PhalApi\DingBot;

/**
 * 扩展入口
 * 为了扩展通用性在支持单例模式的同时不再私有化构造方法和克隆方法
 */

class Lite {

    private $config;
    private $secret;
    private $webHook;
    static private $instance = null;

    /**
     * 构造方法
     * @param $config
     */
    function __construct($config)
    {
        if (is_array($config)){
            $this->config=$config;
            $this->secret=isset($config['secret']) ? $config['secret'] : null ;
            $this->webHook=isset($config['webHook']) ? $config['webHook'] : null ;
        }else{
            $this->config=array();
        }
    }

    /**
     *克隆方法私有化
     */
    function __clone()
    {
        // TODO:Implement __clone() methed.
    }

    /**
     * 获取当前实例
     * @param array $config
     * @return null|Lite
     */
    static public function getInstance($config)
    {
    //判断$instance是否是Singleton的对象，不是则创建
    if (!self::$instance instanceof self) {
        self::$instance = new self($config);
    }
        return self::$instance;
    }

    /**
     * 发送请求
     * @param $post_string
     * @return mixed
     */
    private function request_by_curl($post_string) {
    $ch = curl_init();
    $remote_server=$this->sign_web_hook();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }


    /**
     * 请求签名
     * @return mixed|null|string
     */
    private function sign_web_hook(){
        list($s1, $s2) = explode(' ', microtime());
        $timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $data = $timestamp . "\n" . $this->getSecret();
        $signStr = base64_encode(hash_hmac('sha256', $data, $this->secret,true));
        $signStr = utf8_encode(urlencode($signStr));
        $hookUrl = $this->getWebHook();
        $hookUrl .= "&timestamp=$timestamp&sign=$signStr";
        return $hookUrl;

    }

    /**
     * Text类型消息
     * @param string $content
     * @param array $atMobiles
     * @param bool $isAtAll
     * @return mixed
     */
    public function Text($content, $atMobiles = null, $isAtAll = false){
        $textString['msgtype']='text';
        $textString['text']=array(
            'content'=>$content
        );
        $textString['at']=array(
            'atMobiles'=>$atMobiles,
            'isAtAll'=>$isAtAll
        );
        return $this->request_by_curl(json_encode($textString));

    }

    /**
     * Link类型
     * @param string $text
     * @param string $title
     * @param string $picUrl
     * @param string $messageUrl
     * @return mixed
     */
    public function Link($text, $title,$messageUrl, $picUrl = null ){
        $textString['msgtype']='link';
        $textString['link']=array(
            'text'=>$text,
            "title" => $title,
            "picUrl" => $picUrl,
            "messageUrl" => $messageUrl,
        );
        return $this->request_by_curl(json_encode($textString));
    }

    /**
     * Markdown类型
     * @param string $title
     * @param string $text
     * @param null $atMobiles
     * @param bool $isAtAll
     * @return mixed
     */
    public function Markdown($title, $text, $atMobiles = null, $isAtAll = false){

        $textString['msgtype']='markdown';
        $textString['markdown']=array(
        "title" => $title,
        "text" =>$text
        );
        $textString['at']=array(
        'atMobiles'=>$atMobiles,
        'isAtAll'=>$isAtAll
        );
        return $this->request_by_curl(json_encode($textString));
    }

    /**
     * 整体跳转ActionCard类型
     * @param string $title
     * @param string $text
     * @param string $singleTitle
     * @param string $singleURL
     * @param string $hideAvatar
     * @param string $btnOrientation
     * @return mixed
     */
    public function ActionCard($title, $text, $singleTitle, $singleURL, $hideAvatar = '0', $btnOrientation = '0'){

        $textString['msgtype']='actionCard';
        $textString['actionCard']=array(
            "title" => $title,
            "text" =>$text,
            'btnOrientation'=>$btnOrientation,
            'hideAvatar'=>$hideAvatar,
            'singleTitle'=>$singleTitle,
            'singleURL'=>$singleURL,
        );
        return $this->request_by_curl(json_encode($textString));
    }
    /**
     * 独立跳转ActionCard类型
     * @param string $title
     * @param string $text
     * @param array $btns
     * @param string $hideAvatar
     * @param string $btnOrientation
     * @return mixed
     */
    public function ActionDialog($title, $text, $btns,$hideAvatar = '0', $btnOrientation = '0'){

        $textString['msgtype']='actionCard';
        $textString['actionCard']=array(
            "title" => $title,
            "text" =>$text,
            'btnOrientation'=>$btnOrientation,
            'hideAvatar'=>$hideAvatar,
            'btns'=>$btns
        );
        return $this->request_by_curl(json_encode($textString));
    }
    /**
     * FeedCard类型
     * @param array $links
     * @return mixed
     */
    public function FeedCard($links){
        $textString['msgtype']='feedCard';
        $textString['actionCard']=array(
           'links'=>$links
        );
        return $this->request_by_curl(json_encode($textString));
    }

    /**
     * @return mixed
     */
    public function getSecret()
    {
        return $this->secret;
    }

    /**
     * @param mixed $secret
     */
    public function setSecret($secret)
    {
        $this->secret = $secret;
    }
    /**
     * @return array
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * @param array $config
     */
    public function setConfig($config)
    {
        $this->config = $config;
    }
    /**
     * @return mixed|null
     */
    public function getWebHook()
    {
        return $this->webHook;
    }

    /**
     * @param mixed|null $webHook
     */
    public function setWebHook($webHook)
    {
        $this->webHook = $webHook;
    }
}
